package com.controller;

import com.file.FastDFSFile;
import com.changgou.util.FastDFSClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileController {

    @RequestMapping("/upload")
    public String upload(MultipartFile file){
        try {
        //1、将附件属性信息封装到pojo
        String name = file.getOriginalFilename();       //原始名称
        byte[] content = file.getBytes();               //附件内容
        String ext = FilenameUtils.getExtension(name);  //附件扩展名称
        String md5 = "文文";
        String author = "wenwen";
        FastDFSFile fastDFSFile = new FastDFSFile(name, content, ext, md5, author);
        //2、调用文件上传方法
        String[] uploadResult = FastDFSClient.uploadFile(fastDFSFile);
        //3、拼接图片的url地址
        String url = FastDFSClient.getUrl();
        url = url + "/" + uploadResult[0] + "/" + uploadResult[1];
        return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
